<template>
  <a-modal
    title="价格表"
    width="60%"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleSubmit"
    @cancel="
      () => {
        this.visible = false
      }
    "
  >
    <a-card :bordered="false">
      <!-- <div class="table-operator">
        <a-button v-if="hasPerm('Quot_PriceListMain.Add')" type="primary" icon="plus" @click="handleAdd()"
          >新建</a-button
        >
        <a-button
          v-if="hasPerm('Quot_PriceListMain.Delete')"
          type="primary"
          icon="minus"
          @click="handleDelete(selectedRowKeys)"
          :disabled="!hasSelected()"
          :loading="loading"
          >删除</a-button
        >
        <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
      </div> -->

      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="10">
            <a-col :md="6" :sm="24">
              <a-form-item label="查询类别">
                <a-select allowClear v-model="queryParam.condition">
                  <a-select-option key="ListName">价格表名称</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item>
                <a-input v-model="queryParam.keyword" placeholder="关键字" />
              </a-form-item>
            </a-col>
            <!-- <a-col :md="4" :sm="24">
              <a-form-item>
                <a-range-picker :value="time" @change="onAllocateTimeChange" />
              </a-form-item>
            </a-col> -->

            <a-col :md="6" :sm="24">
              <a-button
                type="primary"
                @click="
                  () => {
                    this.pagination.current = 1
                    this.getDataList()
                  }
                "
                >查询</a-button
              >
              <a-button style="margin-left: 8px" @click="repossess">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <a-table
        ref="table"
        :scroll="{ y: 530 }"
        :columns="columns"
        :rowKey="(row, index) => row.Id"
        :dataSource="data"
        :pagination="pagination"
        :loading="loading"
        @change="handleTableChange"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'radio' }"
        :bordered="true"
        size="small"
      >
        <span slot="CreateTime" slot-scope="CreateTime">{{ timeFormat(CreateTime) }}</span>
        <span slot="UpdateTime" slot-scope="UpdateTime">{{ timeFormat(UpdateTime) }}</span>
        <span slot="StartValidity" slot-scope="StartValidity">{{ timeFormat(StartValidity) }}</span>
        <span slot="EndValidity" slot-scope="EndValidity">{{ timeFormat(EndValidity) }}</span>

        <span slot="action" slot-scope="text, record">
          <template>
            <a v-if="hasPerm('Quot_PriceListMain.Edit')" @click="handleEdit(record.Id)">编辑</a>
            <a-divider
              v-if="hasPerm('Quot_PriceListMain.Edit') && hasPerm('Quot_PriceListMain.Delete')"
              type="vertical"
            />
            <a v-if="hasPerm('Quot_PriceListMain.Delete')" @click="handleDelete([record.Id])">删除</a>
          </template>
        </span>
      </a-table>
    </a-card>
  </a-modal>
</template>

<script>
import moment from 'moment'
const columns = [
  // { title: '备注', dataIndex: 'Remark' },
  { title: '价格表名称', dataIndex: 'ListName' },
  {
    title: '有效期开始时间',
    dataIndex: 'StartValidity',
    align: 'center',
    scopedSlots: { customRender: 'StartValidity' }
  },
  {
    title: '有效期结束时间',
    dataIndex: 'EndValidity',
    align: 'center',
    scopedSlots: { customRender: 'EndValidity' }
  },
  {
    title: '是否采用阶梯价格',
    dataIndex: 'IsLadderPrice',
    align: 'center',

    customRender: (value, row, index) => {
      if (value) return '是'
      if (!value) return '否'
    }
  }
  // { title: '状态', dataIndex: 'State' }
  // {
  //   title: '更新时间',
  //   dataIndex: 'UpdateTime',
  //   align: 'center',
  //   width: '10%',
  //   scopedSlots: { customRender: 'UpdateTime' }
  // },
  // {
  //   title: '创建时间',
  //   dataIndex: 'CreateTime',

  //   align: 'center',
  //   scopedSlots: { customRender: 'CreateTime' }
  // },
  //   { title: '操作', dataIndex: 'action', align: 'left', scopedSlots: { customRender: 'action' } }
]

export default {
  components: {},
  created() {
    this.getDataList()
  },
  data() {
    return {
      time: null,
      data: [],
      visible: false,
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'Id', order: 'desc' },
      loading: false,
      columns,
      queryParam: { condition: 'ListName' },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  methods: {
    handleSubmit() {
      this.$emit('PriceList', this.selectedRows)
      this.visible = false
    },

    repossess() {
      this.queryParam = { condition: 'ListName' }
      this.getDataList()
    },
    handleTableChange(pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }

      this.getDataList()
    },
    getDataList() {
      this.selectedRowKeys = []

      this.loading = true
      this.$http
        .post('/QuotationManagement/Quot_PriceListMain/GetDataListByConditions', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          Search: [this.queryParam],
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    openForm() {
      this.visible = true
      this.selectedRowKeys = []
      this.selectedRows = []
      this.pagination.current = 1
      this.queryParam = { condition: 'ListName' }
      this.getDataList()
    },
    onAllocateTimeChange(dates, dateStrings) {
      this.queryParam.StartDate = dateStrings[0]
      this.queryParam.EndDate = dateStrings[1]

      this.time = dates
      //console.log(this.time)
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRows = selectedRows
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected() {
      return this.selectedRowKeys.length > 0
    },
    handleAdd() {
      this.$refs.editForm.openForm(null, '新建价格表管理信息')
    },
    handleEdit(id) {
      this.$refs.editForm.openForm(id, '编辑价格表管理信息')
    },
    handleDelete(ids) {
      var thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk() {
          return new Promise((resolve, reject) => {
            thisObj.$http.post('/QuotationManagement/Quot_PriceListMain/DeleteDataOfAll', ids).then(resJson => {
              resolve()

              if (resJson.Success) {
                thisObj.$message.success('操作成功!')

                thisObj.getDataList()
              } else {
                thisObj.$message.error(resJson.Msg)
              }
            })
          })
        }
      })
    },
    timeFormat(val) {
      // 时间格式化函数
      return moment(val).format('YYYY-MM-DD')
    }
  }
}
</script>
